package com.qlk.vorder.porxy;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.resteasy.util.Encode;
import org.springframework.beans.factory.annotation.Autowired;

import com.qlk.common.client.AbstractClient;
import com.qlk.common.utils.serialize.JsonHelper;
import com.qlk.vorder.domain.SerialLog;
import com.qlk.vorder.manager.SerialLogManager;
import com.qlk.vorder.porxy.dto.RefundRequest;
import com.qlk.vorder.porxy.dto.RefundResponse;
import com.qlk.vorder.utils.exceptions.Constant;

/**
 * 创建退款单
 * 
 * @author js
 * 
 */
public class CreateRefundClient extends AbstractClient {
	private static final Log LOG = LogFactory.getLog(CreateRefundClient.class);
	@Autowired
	private SerialLogManager serialLogManager;

	/** 创建退款 */
	public boolean createRefund(RefundRequest refundRequest) {
		boolean flag = false;
		boolean flagLog = false;
		SerialLog log = new SerialLog();
		RefundResponse response = null;
		String url = "";
		try {
			url = getServiceUrlDomain() + "/api/refund/create_order";
			PostMethod postMethod = new PostMethod(url);
			String str = JsonHelper.toJson(refundRequest);
			logger.info("创建退款单URL:" + url + "请求报文" + str);
  			// 然后把Soap请求数据添加到PostMethod中
			 NameValuePair[] data = {  
		               new NameValuePair("order_id", refundRequest.getOrder_id()),  
		               new NameValuePair("plat_order_id", refundRequest.getPlat_order_id()),  
		               new NameValuePair("refund_money", String.valueOf(refundRequest.getRefund_money())),  
		               new NameValuePair("user_account",refundRequest.getUser_account()),  
		               new NameValuePair("refund_mode", String.valueOf(refundRequest.getRefund_mode())),
		               new NameValuePair("create_user", refundRequest.getCreate_user()),
		               new NameValuePair("reason", String.valueOf(refundRequest.getReason())),
		               new NameValuePair("mark", refundRequest.getMark()),
		       }; 
			// 最后生成一个HttpClient对象，并发出postMethod请求
			postMethod.setRequestBody(data);
			HttpClient httpClient = new HttpClient();
			int statusCode = httpClient.executeMethod(postMethod);
			logger.info("创建退款接口返回状态statusCode："+statusCode);
			String soapResponseData = postMethod.getResponseBodyAsString();
 			response = JsonHelper.toBean(soapResponseData, RefundResponse.class);
 			response.setMsg(Encode.decode(response.getMsg()));
 			logger.info("创建退款单响应内容:" + soapResponseData);
			if (response != null && statusCode == 200) {
				if (response.getCode() == 0) {
					logger.info("》》》》》》》创建退款单成功 keyWord1:" + refundRequest.getOrder_id());
					log.setRequestStatus(Constant.LOG_STATUS_SUCCESS);
					flag = true;
				} else {
					logger.info("》》》》》》》创建退款单失败 keyWord1:" + refundRequest.getOrder_id());
					log.setRequestStatus(Constant.LOG_STATUS_ERROR);
				}
			}
		} catch (Exception e) {
			LOG.error("调用创建退款端接口异常", e);
		} finally {
			log.setLogType("创建退款单");
			log.setOrderid(refundRequest.getOrder_id());
			log.setRequestContent("请求url:" + url + "请求报文:>>>>" + JsonHelper.toJson(refundRequest));
			log.setResponseContent(JsonHelper.toJson(response));
			flagLog = serialLogManager.insert(log);
			LOG.info("创建退款单流水日志flagLog:"+flagLog);
		}
		return flag;
	}
}
